//
//  TeacherDetailsViewController.m
//  Civacrm
//
//  Created by mubei on 17/2/16.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "TeacherDetailsViewController.h"
#import "LXMPieView.h"
#import "ZFJSegmentedControl.h"
#import "SchoolStudentTableViewCell.h"
#import "GetStudentInfoModel.h"
#import "GetTeacherInfoModel.h"
#import "WithoutDataTableViewCell.h"
#import "BottomView.h"
@interface TeacherDetailsViewController ()
@property (nonatomic, strong) LXMPieView *PieCharViewStudent;
@property (nonatomic, strong) UILabel *firstStudentLable;//学生数据
@property (nonatomic, strong) UILabel *firstMiddelStudentLable;
@property (nonatomic, strong) UILabel *studentLable;
@property (nonatomic, strong) ZFJSegmentedControl *zvc7;
@property (nonatomic, strong) NSMutableArray *studentArray;
@property (nonatomic, strong) NSArray *typeArray;
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *oldType;
@end

@implementation TeacherDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNo = 1;
    self.pageSize = 10;
    self.studentArray = [NSMutableArray array];
    self.typeArray = @[@"-1",@"1",@"0"];
    self.type = @"-1";
    
    [self initNavigation];
    [self initView];
    [self initlowView];
    [self initTableView];
    [self initNetWork:@"-1"];
    
}
- (void)initNavigation{
    UIView *topView = [[UIView alloc] init];
    topView.frame = CGRectMake(0,0, getW(self.navigationController.navigationBar), getH(self.navigationController.navigationBar) );
    UILabel * firstLable = [UILabel labelWithTitle:@"老师激活情况详情" frame:CGRectMake(0,0, 0, 0 ) font:font(32) textColor:WhiteColor bgColor:ClearColor];
    [firstLable sizeToFit];
    firstLable.centerX = topView.width/2 - w(100);
    UILabel *secondLable  = [UILabel labelWithTitle:self.orgName frame:CGRectMake(0, getMaxY(firstLable), 0, 0) font:font(24) textColor:WhiteColor bgColor:ClearColor];
    [secondLable sizeToFit];
    secondLable.centerX = firstLable.centerX;
    [topView addSubview:firstLable];
    [topView addSubview:secondLable];
    
    self.navigationItem.titleView =  topView;
    
}
- (void)initView{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, h(343))];
    topView.backgroundColor = WhiteColor;
    [self.view addSubview:topView];
    
    NSMutableArray *modelArray = [NSMutableArray array];
    CGFloat teacherFloat = 0;
    if (self.schoolPageInfoModel.teacherCounts != 0) {
        teacherFloat = (CGFloat)self.schoolPageInfoModel.teacherActivates /self.schoolPageInfoModel.teacherCounts;
    }
    
    NSArray *value1Array = @[ @(1- teacherFloat), @(teacherFloat)];
    NSArray *color1Array = @[ColorWithHexString(@"def2fa"),
                             ColorWithHexString(@"6dbfee")];
    for (int i = 0 ; i <value1Array.count ; i++) {
        LXMPieModel *model = [[LXMPieModel alloc] initWithColor:color1Array[i] value:[value1Array[i] floatValue] text:nil];
        [modelArray addObject:model];
    }
    
    
    self.PieCharViewStudent = [[LXMPieView alloc] initWithFrame:CGRectMake(w(70),  h(74),w(180), h(180))values:modelArray];
    //self.PieCharViewStudent.delegate = self;
    self.PieCharViewStudent.centerX = self.view.width/2;
    [topView addSubview:self.PieCharViewStudent];
    
    UILabel *bigCircleLable = [UILabel labelWithTitle:nil frame:CGRectMake(w(70)+w(32), getMinY(self.PieCharViewStudent) +h(32), (w(90)-w(32))*2, (w(90)-w(32))*2) font:nil textColor:nil bgColor:WhiteColor];
    bigCircleLable.layer.masksToBounds = YES;
    bigCircleLable.layer.cornerRadius = h(90)-h(32);
    bigCircleLable.center = self.PieCharViewStudent.center;
    [topView addSubview:bigCircleLable];
    
    
        
    self.firstMiddelStudentLable = [[UILabel alloc] init];
    self.firstMiddelStudentLable.frame = CGRectMake(0, 0, 0, 0);
    
    NSString *stringTeacher = [NSString stringWithFormat: @"%ld/%ld",self.schoolPageInfoModel.teacherActivates,self.schoolPageInfoModel.teacherCounts];
    NSString *teacherActivates = [NSString stringWithFormat: @"%ld",self.schoolPageInfoModel.teacherActivates];
    NSMutableAttributedString *mutableAttriteTeaStr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *attributeTeaStr1 = [[NSAttributedString alloc] initWithString:[stringTeacher substringWithRange:NSMakeRange(0, teacherActivates.length)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:((32 / 2) / kScreenHeight6) * kScreenHeight],NSForegroundColorAttributeName : ColorWithHexString(@"6dbfee"),NSBackgroundColorAttributeName :WhiteColor}];
    NSAttributedString *attributeTeaStr4 = [[NSAttributedString alloc] initWithString:[stringTeacher substringWithRange:NSMakeRange(teacherActivates.length, stringTeacher.length - teacherActivates.length)] attributes:@{NSFontAttributeName :font(20),NSForegroundColorAttributeName : ColorWithHexString(@"6dbfee"),NSBackgroundColorAttributeName : WhiteColor}];
    [mutableAttriteTeaStr appendAttributedString:attributeTeaStr1];
    [mutableAttriteTeaStr appendAttributedString:attributeTeaStr4];

    
    self.firstMiddelStudentLable.attributedText = mutableAttriteTeaStr;
    [self.firstMiddelStudentLable sizeToFit];
    self.firstMiddelStudentLable.center = bigCircleLable.center;
    [topView addSubview:self.firstMiddelStudentLable];
    
    self.studentLable = [UILabel labelWithTitle:@"教师激活情况" frame:CGRectMake(getMinX(self.PieCharViewStudent), getMaxY(self.PieCharViewStudent) + h(21), 0, 0) font:font(24) textColor:ColorWithHexString(@"6dbfee") bgColor:nil];
    [self.studentLable sizeToFit];
    self.studentLable.centerX = self.PieCharViewStudent.centerX;
    [topView addSubview:self.studentLable];


    
}
- (void)initlowView{
    NSArray *titleArr = @[@"全部",@"已激活",@"未激活"];
    self.zvc7 = [[ZFJSegmentedControl alloc]initwithTitleArr:titleArr iconArr:nil SCType:SCType_Underline];
    self.zvc7.frame = CGRectMake(0, getMaxY(self.studentLable) + h(43),self.view.width, h(86));
    self.zvc7.centerX = self.view.width/2;
    self.zvc7.backgroundColor = [UIColor whiteColor];
    self.zvc7.titleColor = ColorWithHexString(@"98c1d4");
    self.zvc7.selectBtnSpace = 0;//设置按钮间的间距
    //    zvc7.selectBtnWID = ;//设置按钮的宽度 不设就是均分
    self.zvc7.titleFont = font(24);//按钮的常态样式
    self.zvc7.selectTitleColor = GlobalBlue;
    self.zvc7.SCType_Underline_HEI = 2;//设置底部横线的高度
    __weak TeacherDetailsViewController *weakSelf = self;
    
    self.zvc7.selectType = ^(NSInteger selectIndex,NSString *selectIndexTitle){
        NSLog(@"selectIndexTitle == %@",selectIndexTitle);
        weakSelf.type = weakSelf.typeArray[selectIndex];
        [weakSelf initNetWork:weakSelf.type];
        [weakSelf.tableView setContentOffset:CGPointMake(0,0) animated:NO];
        

        
    };
    [self.view addSubview:self.zvc7];
    
}
- (void)initTableView{
    self.tableView.frame = CGRectMake(0, getMaxY(self.zvc7), self.view.width, self.view.height - getMaxY(self.zvc7) - h(80) - 64);
    [self.tableView registerClass:[SchoolStudentTableViewCell  class] forCellReuseIdentifier:@"Cell"];
    [self.tableView registerClass:[WithoutDataTableViewCell  class] forCellReuseIdentifier:@"item"];
    self.tableView.mj_footer = self.footer;
    [self.footer  setTitle:@"已经到底啦" forState:MJRefreshStateNoMoreData];
    self.footer .stateLabel.font = font(24);
    self.footer.stateLabel.textColor = ColorWithHexString(@"cfdfe8");
    

    

    BottomView * bottomView = [BottomView initBottomViewWithFrame:CGRectMake(0, kScreenHeight - h(80) - 64, kScreenWidth, h(80)) andActivateNumString:self.activeString andPayForNumString:self.payforString];
    [self.view addSubview:bottomView];
    


    [self.view addSubview:self.tableView];
}
#pragma mark tabelView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.studentArray.count>0) {
        tableView.scrollEnabled = YES;
        
        SchoolStudentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.getTeacherInfoModel = self.studentArray[indexPath.row];
        return cell;
    }else{
        tableView.scrollEnabled = NO;
        WithoutDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"item"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.studentArray.count>0) {
        return self.studentArray.count;
        
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.studentArray.count>0) {
        return h(114);
        
    }else{
        return self.view.height - getMaxY(self.zvc7) - 50;
    }
}
#pragma mark 下拉刷新和上拉加载

- (void)handleHeaderRefreshingAction{
    
    self.pageNo = 1;
    [self initNetWork:self.type];
}

- (void)handleFooterRefreshingAction{
    
    self.pageNo ++;
    [self initNetWork:self.type];
    
}
- (void)initNetWork:(NSString *)type{
    //
    if (![self.oldType isEqualToString:type]) {
        self.pageNo = 1;
    }
    self.oldType = type;
    self.params[@"type"] = type;
    self.params[@"pageNo"] = [NSString stringWithFormat:@"%@",@(self.pageNo)];
    self.params[@"pageSize"] = [NSString stringWithFormat:@"%@",@(self.pageSize)];
    //    self.userID = @"6d40e15e-7433-416f-9d9b-13299ea4f411";∫
    [self showIndeterminate];
    [self.manager postWithURL:NET_GETTECHERINFO param:self.params delegate:self isEncrypt:NO type:NetWorkingType0];
    
    
}
- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    switch (type) {
        case NetWorkingType0:
        {
            [self endRefresh];

            if (self.pageNo == 1) {
                [self.studentArray removeAllObjects];
            }
            
            NSArray *array  = [responseObject objectForKey:@"items"];
            for (NSDictionary *dic in array) {
                GetTeacherInfoModel *model = [[GetTeacherInfoModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.studentArray addObject:model];
            }

            if (array.count < self.pageSize) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                

            }
            [self.tableView reloadData];
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type{
    [self hideHud];
    [self endRefresh];

}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    [self endRefresh];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
